package com.ftg.learn.mapper;

import com.ftg.learn.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kangjx
 * @createTime 2022/11/25
 */
public class CallStudent1Mapper {

    /**
     * 我们可以通过接口  + sqlmapper.xml 的方式开发，要求是什么？sqlmapper namespace 要是一个真实的接口
     *   1 ：  路径 要完成正确
     *   2 : sql语句的id要与路径对应的方法完全一致
     *   3 : 接口里面方法不能重载
     * @param args
     */
    public static void main(String[] args) {
        String path = "mybatisconfig.xml";

        try {
            Reader reader = Resources.getResourceAsReader(path);

            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);

            SqlSession session = ssf.openSession();

            StudentMapper1 studentMapper1 = session.getMapper(StudentMapper1.class);

            Student stu = new Student();
            stu.setSno("s033");
            stu.setSname("张小荷");
            stu.setSage(13);
            stu.setSsex("女");

//            Map<String,Object> map = new HashMap<>();
//            map.put("sno","s023");
//            map.put("sname","孙铁蛋");
//            map.put("sage",13);
//            map.put("ss1ex","男");
//            int i = studentMapper1.saveStu(stu);
//            System.out.println("添加行数" + i);

//            studentMapper1.modify(stu);

            studentMapper1.delStu("s033");


            System.out.println("showall1 我是接口里注解 3 ==>" + studentMapper1.showall1());
            System.out.println("接口没有注解  sql从配置文件拿" + studentMapper1.showall());

            session.commit();
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
